package com.alibaba.citrus.cr.unified.inventory.facade.service;

import com.alibaba.citrus.cr.unified.inventory.contants.MessageHelperUtil;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.middleware.mq.runtime.EpochMessageService;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.contants.BcOrderConstants;
import com.epoch.app.crunifiedinventory.allocateorderstandardmessage.service.AllocateOrderStandardMessageService;
import com.epoch.app.crunifiedinventory.model.dto.AllocateOrderStandardMessageDTO;
import com.google.common.collect.Maps;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author yifei
 * @since 2024/3/7
 */
@Service
@Primary
public class AllocateOrderStandardMessageServiceImpl implements AllocateOrderStandardMessageService {
    private static final Log log = Log.getLogger(AllocateOrderStandardMessageServiceImpl.class);

    @Resource
    private EpochMessageService epochMessageService;

    @Override
    public Result<String> sendAllocateOrderStandardMessage(AllocateOrderStandardMessageDTO dto) {
        try {
            if (!MessageHelperUtil.checkAllocateOrderStandardMessageDTO(dto)) {
                return Result.fail("OTS-03-005-00-15-251", "调拨单标准消息入参校验不合法");
            }
//            String messageKey = MessageHelperUtil.buildMessageKey(dto);
            String messageKey = dto.getKeyword();
            log.info("AllocateOrderStandardMessageServiceImpl_sendAllocateOrderStandardMessage_messageKey:{}", messageKey);
            Map<String, String> map = Maps.newHashMap();
            map.put("_CZ_KEY", BcOrderConstants.TENANT_ID());
            Boolean result = epochMessageService.sendMessage("allocate_order_standard_message", "CZ_ALLOCATE_V2",
                    "AllocateStandardMessage", messageKey, JSON.toJSONString(dto), map);
            log.info("AllocateOrderStandardMessageServiceImpl_sendAllocateOrderStandardMessage_result:{}",result);
            if (result == null || !result) {
                return Result.fail("OTS-03-005-00-15-252", "调拨单标准消息发送失败");
            }
            return Result.success("");
        } catch (Throwable t) {
            log.error("AllocateOrderStandardMessageServiceImpl_sendAllocateOrderStandardMessage_error:", t);
            return Result.fail("OTS-03-005-00-15-252", "调拨单标准消息发送失败");
        }
    }
}
